import { createSlice} from "@reduxjs/toolkit";
import { useSelector} from "react-redux";
import { RootSate } from "..";
import { useDispatch } from "react-redux";
import { DataType } from "../../types";
const initialState:{lists:DataType[]}= {lists:[]};
export const listSlice = createSlice({
    name:"listSlice",
    initialState,
    reducers:{
        saveLists: (state,{payload})=>{
            state.lists=payload
        },
        addItem:(state,{payload})=>{
            state.lists.push(payload)
        }
    }

})

export const useLists = ()=>{
    const lists=useSelector((state:RootSate)=>state.listReducer.lists)
    const dispatch = useDispatch()
    const loadList=(parmas:DataType[])=>dispatch(listSlice.actions.saveLists(parmas));
    const addItem=(params:DataType)=>dispatch(listSlice.actions.addItem(params))
    return {lists,loadList,addItem}
}